package com.oto.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Service;

import com.oto.model.FlowDO;
import com.oto.util.DateUtils;

/**
 * @author xueqiang.mi
 * @since 2010-11-27
 */
@Service
public class FlowService extends BaseService {

	public void add(FlowDO f) {
		Date now = DateUtils.now();
		f.setCrtDate(now);
		f.setModDate(now);

		save(f);
	}

	public void del(Long id) {
		delete(FlowDO.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> digg() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("dataStatus > 0");
			query.setOrdering("rating desc");
			query.setRange(0, 15);
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute());
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	public FlowDO get(Long id) {
		return get(FlowDO.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> getByPack(long packId) {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("packId == :packId && dataStatus > -1");
			query.setOrdering("dataStatus asc, crtDate desc");
			query.setRange(0, 15);
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute(packId));
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> getLattestByUser(String userName, int number) {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("owner == :owner && dataStatus > -1");
			query.setOrdering("dataStatus asc, crtDate desc");
			number = (number == 0) ? 15 : number;
			query.setRange(0, number);
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute(userName));
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> hottest() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("dataStatus > 0");
			query.setOrdering("dataStatus asc, rating desc");
			query.setRange(0, 15);
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute());
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> lattest() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("dataStatus > 0");
			query.setOrdering("dataStatus asc, crtDate desc");
			query.setRange(0, 15);
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute());
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	public List<FlowDO> latestUnsettled(int number) {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("dataStatus == 1");
			query.setOrdering("crtDate asc");
			query.setRange(0, number);
			@SuppressWarnings("unchecked")
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute());
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}

	public List<FlowDO> list() {
		return list(FlowDO.class);
	}

	public void setDataStatus(Long id, int status) {
		PersistenceManager pm = PMF.get();
		try {
			FlowDO obj = pm.getObjectById(FlowDO.class, id);
			obj.setDataStatus(status);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<FlowDO> getSyncFlows(String user) {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(FlowDO.class);
			query.setFilter("owner == :owner && status == 0");
			query.setOrdering("crtDate desc");
			Collection<FlowDO> c = pm.detachCopyAll((Collection<FlowDO>) query.execute(user));
			return new ArrayList<FlowDO>(c);
		} finally {
			pm.close();
		}
	}
}
